package com.heima.freemarker.controller.v1;

import com.heima.freemarker.pojos.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

@Controller
@RequestMapping("/hello")
public class HelloController {
	@GetMapping
	public String hello(Model model) {
		//1.纯文本形式的参数
		model.addAttribute("name", "张三");
		//2.实体类相关的参数
		Student student = new Student();
		student.setName("阿明");
		student.setAge(18);
		model.addAttribute("stu", student);
		return "01-basic";
	}

	@GetMapping("/modelMap")
	public String hello(ModelMap model) {
		//1.纯文本形式的参数
		// model.addAttribute("name", "张三");
		//2.实体类相关的参数
		model.put("name", "李狗");
		Student student = new Student();
		student.setName("阿明");
		student.setAge(18);
		model.addAttribute("stu", student);
		return "01-basic";
	}

	@GetMapping("/mav")
	public ModelAndView hello() {
		ModelAndView mav = new ModelAndView();
		//封装数据
		mav.addObject("name", "张三");
		Student student = new Student();
		student.setName("狗子");
		student.setAge(18);
		mav.addObject("stu", student);
		//跳转页面
		mav.setViewName("01-basic");
		return mav;
	}

	@GetMapping("list")
	public String list(ModelMap modelMap) {
		//封装数据
		Student student1 = new Student();
		student1.setName("大狗子");
		student1.setAge(10);
		student1.setMoney(100f);

		Student student2 = new Student();
		student2.setName("二狗子");
		student2.setAge(20);
		student2.setMoney(200f);

		Student student3 = new Student();
		student3.setName("三狗子");
		student3.setAge(30);
		student3.setMoney(300f);

		List list = new ArrayList();
		list.add(student1);
		list.add(student2);
		list.add(student3);

		modelMap.put("studentlist", list);
		//map封装
		Map resultMap = new HashMap();
		resultMap.put("stu1", student1);
		resultMap.put("stu2", student2);
		resultMap.put("stu3", student3);
		modelMap.put("resultMap", resultMap);
		return "02-list";
	}
}
